package com.example.onlineeducation.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.example.onlineeducation.R
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.tools.SdkVersionUtils
import com.yc.jetpacklib.extension.ycLoadImageNetFilletDp
import com.yc.jetpacklib.extension.ycLoadImageRes
import com.yc.jetpacklib.widget.YcDrawableTextView
import java.util.*

/**
 * author：luck
 * project：PictureSelector
 * package：com.luck.pictureselector.adapter
 * email：893855882@qq.com
 * data：16/7/27
 */
class SignBehalfAddPictureAdapter(context: Context, private val maxImages: Int = 1) : RecyclerView.Adapter<SignBehalfAddPictureAdapter.ViewHolder?>() {

    private val mInflater: LayoutInflater = LayoutInflater.from(context)
    private var list: MutableList<LocalMedia>? = ArrayList<LocalMedia>()
    var itemClick: ((position: Int, v: View?) -> Unit)? = null
    var removeClick: ((Int) -> Unit)? = null
    var addPictureClick: ((Int) -> Unit)? = null


    override fun getItemCount(): Int {
        return if (list?.size!! < maxImages) {
            list?.size!! + 1
        } else {
            list?.size!!
        }
    }

    companion object {
        const val TYPE_CAMERA = 1
        const val TYPE_PICTURE = 2
    }

    fun setList(list: MutableList<LocalMedia>?) {
        this.list = list
    }

    val data: List<Any>
        get() = if (list == null) ArrayList() else list!!

    fun remove(position: Int) {
        if (list != null) {
            list!!.removeAt(position)
        }
    }

    fun clear() {
        if (list != null) {
            list!!.clear()
        }
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var ivPicture: ImageView = view.findViewById(R.id.ivPicture)
        var ivDelete: ImageView = view.findViewById(R.id.ivDelete)
        var dtvUploadPicture: ImageView = view.findViewById(R.id.dtvUploadPicture)
    }


    override fun getItemViewType(position: Int): Int {
        return if (position == list!!.size) {
            TYPE_CAMERA
        } else {
            TYPE_PICTURE
        }
    }

    /**
     * 创建ViewHolder
     */
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view: View = mInflater.inflate(
            R.layout.add_picture_small_item,
            viewGroup, false
        )
        return ViewHolder(view)
    }

//    private fun isShowAddItem(position: Int): Boolean {
//        return
//    }

    /**
     * 设置值
     */
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        //少于xx张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.apply {
                dtvUploadPicture.visibility = View.VISIBLE
                dtvUploadPicture.setOnClickListener { addPictureClick?.invoke(position) }
                ivDelete.visibility = View.INVISIBLE
                ivPicture.ycLoadImageRes(R.drawable.shape_round10_grey)
            }
        } else {
            viewHolder.apply {
                dtvUploadPicture.visibility = View.GONE
                ivDelete.visibility = View.VISIBLE
                ivDelete.setOnClickListener {
                    val index: Int = viewHolder.adapterPosition
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        remove(index)
                        notifyItemRemoved(index)
                        notifyItemRangeChanged(index, list!!.size)
                        removeClick?.invoke(index)
                    }
                }
            }
            val media: LocalMedia = list!![position]
            val imgPath: String = if (media.isCut && !media.isCompressed) { // 裁剪过
                media.cutPath
            } else if (media.isCompressed || media.isCut && media.isCompressed) { // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                media.compressPath
            } else { // 原图
                if (SdkVersionUtils.checkedAndroid_Q()) media.androidQToPath else media.path
            }
            // 图片
//            if (media.isCompressed) {
//                ycLogDSimple(tag = "compress image result:", msg = (File(media.compressPath).length() / 1024).toString() + "k")
//                ycLogDSimple(tag = "压缩地址::", msg = media.compressPath)
//            }
//            ycLogDSimple(tag = "原图地址::", msg = media.path)
//            if (media.isCut) {
//                ycLogDSimple(tag = "裁剪地址::", msg = media.cutPath)
//            }

            viewHolder.ivPicture.ycLoadImageNetFilletDp(imgPath, 10f)
            viewHolder.itemView.setOnClickListener { v: View? ->
                itemClick?.invoke(viewHolder.adapterPosition, v)
            }
        }
    }

}